package com.b1.taskmanager.start.gui;

import com.b1.taskmanager.api.Contact;
import com.b1.taskmanager.api.ContactList;
import com.b1.taskmanager.api.Task;
import com.b1.taskmanager.api.TaskList;
import java.awt.AWTError;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.plaf.metal.MetalBorders;
import javax.swing.table.DefaultTableModel;

public class ChangeWindow extends JFrame
{
    private JButton button;
    private JPanel panel;
    private JTable table;
    private JScrollPane scroll;
    private JLabel label1;
    private JLabel label2; 
    private JLabel label3;
    private JComboBox comboContacts;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField4;
    private JTextArea textArea;
    private JRadioButton radioMeeting;
    private JRadioButton radioMemo;
    private ContactList ctList = new ContactList();
    private HashMap<String, String> contactMap = new HashMap<String, String>();
    private int globalId;

    public ChangeWindow(int i,int id) 
    {
        super("Change");
        globalId = id;
        switch(i)
        {
            case 0:
                changeTask();
                break;
            case 1:
                changeContact();
                break;
        }       
        
    }
    
    private void changeTask()
    {
        this.setTitle("Change Task");
        this.setSize(300, 400); 
        try 
        {
            java.awt.Toolkit jToolkit = java.awt.Toolkit.getDefaultToolkit();
            Dimension screenSize = jToolkit.getScreenSize();
            this.setLocation((int) (screenSize.getWidth()/4), (int) (screenSize.getHeight()/4));
        } 
        catch (AWTError ex) 
        {
        }
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        panel = new JPanel();
        panel.setLayout(null);
        
        TaskList tl = new TaskList();
        Task task = tl.getTaskByNumber(globalId);
        
        
        
        label1  = new JLabel("Title Task");
        label1.setSize(60,14);
        label1.setLocation(20, 10);
        textField1 = new JTextField();
        textField1.setSize(100,20);
        textField1.setLocation(20,25);
        textField1.setName("Title");
        textField1.setText(task.getTitle());
        panel.add(label1);
        panel.add(textField1);
        
        label1  = new JLabel("Change contact");
        label1.setSize(90,14);
        label1.setLocation(150, 10);
        panel.add(label1);
        
        for (int i = 0; i < ctList.getContactList().size(); i++) 
        {
            contactMap.put(ctList.listToString()[i][1], ctList.listToString()[i][0]);   
        }
        Set<String> myset = contactMap.keySet();
        String[] dataValue = (String[]) myset.toArray(new String[myset.size()]);
        comboContacts = new JComboBox(dataValue);
        comboContacts.setSize(100,20);
        comboContacts.setLocation(150,25);        
        comboContacts.addActionListener(new ActionListener() 
        {
	           public void actionPerformed(ActionEvent e) 
                   {
                       JComboBox box = (JComboBox) e.getSource();
                       String item = contactMap.get((String) box.getSelectedItem());
                       label3 = new JLabel();
                       label3.setText(item);
                   }
        });
        panel.add(comboContacts);
        SimpleDateFormat dateformat = new SimpleDateFormat("HH:mm dd-MM-yyyy");
        label1  = new JLabel("Date begin");
        label1.setSize(60,14);
        label1.setLocation(20, 50);
        textField2 = new JTextField();
        textField2.setSize(100,20);
        textField2.setLocation(20,65);
        textField2.setName("Begin Date");
        textField2.setText(new StringBuilder(dateformat.format(task.getBeginDate())).toString());
        panel.add(label1);
        panel.add(textField2);
        
        label1  = new JLabel("End begin");
        label1.setSize(60,14);
        label1.setLocation(120, 50);
        textField4 = new JTextField();
        textField4.setSize(100,20);
        textField4.setLocation(120,65);
        textField4.setName("End Date");
        textField4.setText(new StringBuilder(dateformat.format(task.getEndDate())).toString());
        panel.add(label1);
        panel.add(textField4);
        
        label1  = new JLabel("Description");
        label1.setSize(80,14);
        label1.setLocation(20, 90);
        textArea = new JTextArea();
        textArea.setSize(200,80);
        textArea.setLocation(20,105);
        textArea.setName("Description");
        textArea.setText(task.getDescription());
        Border br = new MetalBorders.TextFieldBorder();
        textArea.setBorder(br);
        panel.add(label1);
        panel.add(textArea);
        
        label2  = new JLabel("Place");
        label2 .setSize(50,14);
        label2 .setLocation(20, 220);
        textField3 = new JTextField();
        textField3.setSize(100,20);
        textField3.setLocation(20,235);
        textField3.setName("Place");
 
        textField3.setText(task.getPlace());
        label2 .setVisible(false);
        textField3.setVisible(false);
        panel.add(label2 );
        panel.add(textField3);
        
        
        ButtonGroup bGroup = new ButtonGroup();
        radioMeeting = new JRadioButton("Meeting");
        radioMeeting.setSize(80, 14);
        radioMeeting.setLocation(20, 200);
        radioMeeting.addActionListener(new ActionListener() 
                {
	            public void actionPerformed(ActionEvent e) 
                    {
	                if(radioMeeting.isEnabled())
                        {
                            label2.setVisible(true);
                            textField3.setVisible(true);
                        }
	            }
	        });
        radioMemo = new JRadioButton("Memo");
        radioMemo.setLocation(100, 200);
        radioMemo.setSize(90, 14);
        radioMemo.addActionListener(new ActionListener() 
                {
	            // соответственно, для любого нажатия...
	            public void actionPerformed(ActionEvent e) 
                    {
	                if(radioMemo.isEnabled())
                        {
                            label2.setVisible(false);
                            textField3.setVisible(false);
                        }
	            }
	        });
        bGroup.add(radioMeeting);
        bGroup.add(radioMemo);
        panel.add(radioMeeting);
        panel.add(radioMemo);
        if(task.getType().equals("meeting")) 
        {
            radioMeeting.setSelected(true);
            textField3.setVisible(true);
        }
        else radioMemo.setSelected(true);
        
        button = new JButton("Change Task");
        button.setLocation(20,270);
        button.setSize(135,40);
        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e) 
            {
                try 
                {
                    TaskList ts = new TaskList();
                    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("HH:mm dd-MM-yyyy");
                    String type = "memo";
                    if (radioMeeting.hasFocus()) {
                        type = "meeting";
                        textField3.setText("-");
                    }
                    List<Contact> list = new ArrayList<Contact>();
                    list.add(ctList.getContactByNumber(Integer.parseInt(label3.getText())));
                    ts.cahngeTask(globalId,sdf.parse(textField2.getText()),sdf.parse(textField4.getText()),
                            textField1.getText(),type,textArea.getText(),textField3.getText(),list);
                    MainWindow mw = new MainWindow();
                    String[] str = {textField1.getText(), textField2.getText(), textArea.getText()};
                    DefaultTableModel newModel = (DefaultTableModel) mw.getTableModel();
                    mw.setTableModel(newModel);
                    mw.setVisible(true);
                    setVisible(false);
                } 
                catch (ParseException ex) {
                    Logger.getLogger(AddWindow.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        panel.add(button);
        
        
        
        this.setContentPane(panel);
    }
    
    
    public void changeContact()
    {
        this.setTitle("Change Contact");
        this.setSize(300, 200); 
        try 
        {
            java.awt.Toolkit jToolkit = java.awt.Toolkit.getDefaultToolkit();
            Dimension screenSize = jToolkit.getScreenSize();
            this.setLocation((int) (screenSize.getWidth()/4), (int) (screenSize.getHeight()/4));
        } 
        catch (AWTError ex) 
        {
        }
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        panel = new JPanel();
        panel.setLayout(null);
        
        
        ContactList cl = new ContactList();
        Contact contact = cl.getContactByNumber(globalId);
        
        label1  = new JLabel("First Name");
        label1.setSize(80,14);
        label1.setLocation(20, 10);
        textField1 = new JTextField();
        textField1.setSize(100,20);
        textField1.setLocation(20,25);
        textField1.setName("first name");
        textField1.setText(contact.getFirstName());
        panel.add(label1);
        panel.add(textField1);
        
        label1  = new JLabel("Last name");
        label1.setSize(60,14);
        label1.setLocation(20, 50);
        textField2 = new JTextField();
        textField2.setSize(100,20);
        textField2.setLocation(20,65);
        textField2.setName("last name");
        textField2.setText(contact.getLastName());
        panel.add(label1);
        panel.add(textField2);
        
        label1  = new JLabel("Phone");
        label1.setSize(80,14);
        label1.setLocation(20, 90);
        textArea = new JTextArea();
        textArea.setSize(80,20);
        textArea.setLocation(20,105);
        textArea.setName("phone");
        textArea.setText(contact.getHomePhone());
        Border br = new MetalBorders.TextFieldBorder();
        textArea.setBorder(br);
        panel.add(label1);
        panel.add(textArea);        
        
        button = new JButton("Change contact");
        button.setLocation(140,20);
        button.setSize(140,40);
        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e) 
            {
                ContactList cl = new ContactList();
                cl.cahngeContact(globalId, textField1.getText(), textField2.getText(),  textArea.getText());
                ShowContact sc = new ShowContact();
                String [] str = {textField1.getText(),textField2.getText(), textArea.getText()};
                DefaultTableModel newModel = (DefaultTableModel) sc.getTableModel();
                //newModel.addRow(str);
                sc.setTableModel(newModel);
                sc.setVisible(true);
                setVisible(false);
            }
        });
        panel.add(button);

        
        
        
        this.setContentPane(panel);
    }
}
